package com.intelligent.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.intelligent.pojo.Process;
import com.intelligent.service.ProcessService;
import com.intelligent.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


@RestController
@RequestMapping("/process")
public class ProcessController {

    @Autowired
    private ProcessService processService;

    /**
     * 根据ID查询工序信息
     * @param processId 工序ID
     * @return 工序详细信息
     */
    @GetMapping("/{processId}")
    public Map<String, Object> getProcessById(@PathVariable Long processId) {
        try {
            return processService.getProcessById(processId);
        } catch (IllegalArgumentException e) {
            throw new RuntimeException("查询失败：" + e.getMessage());
        } catch (Exception e) {
            throw new RuntimeException("查询失败：" + e.getMessage());
        }
    }

    /**
     * 分页查询工序信息
     * @param current 当前页
     * @param size 每页大小
     * @param processCode 工序编号（可选）
     * @param processName 工序名称（可选）
     * @param processType 工序类型（可选）
     * @param status 状态（可选）
     * @return 分页结果
     */
    @GetMapping("/page")
    public IPage<Map<String, Object>> getProcessPage(
            @RequestParam(defaultValue = "1") Long current,
            @RequestParam(defaultValue = "10") Long size,
            @RequestParam(required = false) String processCode,
            @RequestParam(required = false) String processName,
            @RequestParam(required = false) Integer processType,
            @RequestParam(required = false) Integer status) {

        Page<Map<String, Object>> page = new Page<>(current, size);
        return processService.getProcessPage(page, processCode, processName, processType, status);
    }

    /**
     * 添加工序信息
     * @param process 工序信息
     * @return 添加结果
     */
    @PostMapping("/add")
    public Result<Boolean> addProcess(@RequestBody Process process) {
        boolean success = processService.addProcess(process);
        return success ? Result.success(true) : Result.error("添加工序信息失败");
    }

    /**
     * 修改工序信息
     * @param process 工序信息
     * @return 修改结果
     */
    @PutMapping("/update")
    public String updateProcess(@RequestBody Process process) {
        try {
            boolean result = processService.updateProcess(process);
            if (result) {
                return "工序信息修改成功";
            } else {
                return "工序信息修改失败";
            }
        } catch (IllegalArgumentException e) {
            return "工序信息修改失败：" + e.getMessage();
        } catch (Exception e) {
            return "工序信息修改失败：" + e.getMessage();
        }
    }

    /**
     * 删除工序信息
     * @param processId 工序ID
     * @return 删除结果
     */
    @DeleteMapping("/delete/{processId}")
    public String deleteProcess(@PathVariable Long processId) {
        try {
            boolean result = processService.deleteProcess(processId);
            if (result) {
                return "工序信息删除成功";
            } else {
                return "工序信息删除失败";
            }
        } catch (IllegalArgumentException e) {
            return "工序信息删除失败：" + e.getMessage();
        } catch (Exception e) {
            return "工序信息删除失败：" + e.getMessage();
        }
    }

}
